package com.zxl.grocery.modular.schedule.service;

import com.zxl.grocery.modular.crawler.base.AntAction;
import com.zxl.grocery.modular.crawler.base.AntInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 爬虫定时Service
 *
 * @author zxl
 * @Date 2017-08-24 10:27:44
 */
@Service
public class CrawlerTaskService {

    @Autowired
    Map<String, AntAction> antActionMap;

    public AntInfo queryObject(String id) {
        AntAction antAction = antActionMap.get(id);
        AntInfo antInfo = new AntInfo();
        antInfo.setId(id);
        antInfo.setStatus(antAction.status());
        antInfo.setName(antAction.getClass().getName());
        return antInfo;
    }

    public List<AntInfo> queryList(Map<String, Object> map) {
        List<AntInfo> AntInfos = new ArrayList<AntInfo>();
        for (Map.Entry<String, AntAction> entry : antActionMap.entrySet()) {
            AntAction antAction = entry.getValue();
            String id = entry.getKey();
            AntInfo antInfo = new AntInfo();
            antInfo.setId(id);
            antInfo.setStatus(antAction.status());
            antInfo.setName(antAction.getClass().getName());
            AntInfos.add(antInfo);
        }
        return AntInfos;
    }

    public int queryTotal(Map<String, Object> map) {

        return antActionMap.size();
    }

    public void start(String id) {
        AntAction antAction = antActionMap.get(id);
        antAction.start();

    }

    public void restart(String id) {
        AntAction antAction = antActionMap.get(id);
        antAction.restart();
    }

    public void stop(String id) {
        AntAction antAction = antActionMap.get(id);
        antAction.stop();
    }

    public void start(String[] ids) {
        for (String id : ids) {
            this.start(id);
        }
    }

    public void restart(String[] ids) {
        for (String id : ids) {
            this.restart(id);
        }
    }

    public void stop(String[] ids) {
        for (String id : ids) {
            this.stop(id);
        }
    }

}
